package com.littlebee.storysuper.utils;

import android.app.Activity;
import android.content.Context;
import android.os.Process;
import android.widget.Toast;

import java.util.Timer;
import java.util.TimerTask;

/**
 * 提示公有类
 */
public class CommonToastUtils {
    private static boolean isExist = false;

    public CommonToastUtils() {
    }

    public static void showInCenterToast(Context context, String msg) {
        showInCenterToast(context, msg, 0);
    }

    public static void showInCenterToast(Context context, int resId) {
        showInCenterToast(context, context.getString(resId), 0);
    }

    public static void showInCenterToast(Context context, String msg, int duration) {
        Toast.makeText(context, msg, duration).show();
    }

    public static void exitClient(Activity context, boolean isExit) {
        if(isExit) {
            Timer timer = null;
            if(!isExist) {
                isExist = true;
                showInCenterToast(context, "再按一次退出程序", 1);
                timer = new Timer();
                timer.schedule(new TimerTask() {
                    public void run() {
                        CommonToastUtils.isExist = false;
                    }
                }, 2000L);
            } else {
                Process.killProcess(Process.myPid());
                System.exit(0);
                Runtime.getRuntime().exit(0);
            }
        } else {
            context.moveTaskToBack(true);
        }

    }
}
